package com.manong.bookstore.order.service.impl;

import org.springframework.stereotype.Service;

import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manong.common.utils.PageUtils;
import com.manong.common.utils.Query;

import com.manong.bookstore.order.dao.OrderDao;
import com.manong.bookstore.order.entity.OrderEntity;
import com.manong.bookstore.order.service.OrderService;
import org.springframework.util.StringUtils;


@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderDao, OrderEntity> implements OrderService {

//    @Override
//    public PageUtils queryPage(Map<String, Object> params) {
//        IPage<OrderEntity> page = this.page(
//                new Query<OrderEntity>().getPage(params),
//                new QueryWrapper<OrderEntity>()
//        );
//
//        return new PageUtils(page);
//    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //1、获取key
        String key = (String) params.get("key");
        QueryWrapper<OrderEntity> queryWrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(key)){
            queryWrapper.eq("order_id",key)
                    .or().eq("user_id",key)
                    .or().eq("store_id",key)
                    .or().eq("order_amount",key)
                    .or().like("buyer_name",key)
                    .or().like("buyer_phone",key)
                    .or().like("buyer_address",key)
                    .or().like("order_status",key)
                    .or().like("pay_status",key);
        }

        IPage<OrderEntity> page = this.page(
                new Query<OrderEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long storeId) {
        String key = (String) params.get("key");
        QueryWrapper<OrderEntity> wrapper=new QueryWrapper<OrderEntity>().eq("store_id",storeId);
        if(!StringUtils.isEmpty(key)){
            wrapper.and((obj)->{
                obj.eq("order_id",Integer.parseInt(key)).or().like("buyer_name",key).or().like("buyer_address",key);
            });
        }
        IPage<OrderEntity> page=this.page(new Query<OrderEntity>().getPage(params),wrapper);
//        List<OrderEntity> list = page.getRecords();
//        String limit= (String) params.get("limit");
//        Page pp = (Page) params.get("page");
//        if(limit!=null && pp!=null){
//            int pageSize = Integer.parseInt(params.get("limit").toString());
//            int currentPage = (int) pp.getCurrent();
//
//            int total = list.size();
//
//            page.setRecords(list.subList((currentPage - 1) * pageSize,
//                    (currentPage * pageSize) < list.size() ? (currentPage * pageSize) : list.size()));
//            page.setTotal(total);
//        }
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage2(Map<String, Object> params, Long userId) {
        QueryWrapper<OrderEntity> wrapper=new QueryWrapper<OrderEntity>().eq("user_id",userId);
        IPage<OrderEntity> page=this.page(new Query<OrderEntity>().getPage(params),wrapper);
        return new PageUtils(page);
    }

}